package com.niit.controller;

import com.niit.pojo.Window;
import com.niit.service.WindowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
public class WindowController {

    @Autowired
    private WindowService windowService; // 自动注入 WindowService 依赖




    @GetMapping("/backend-api-endpoint/getAllWindows")
    @ResponseBody
    public List<Window> getAllWindows() {
        // 调用服务从数据库获取数据
        List<Window> windows = windowService.getAllWindows();
        return windows;
    }


    @RequestMapping("/window_table")
    public String window_table(){
        return "window_table";
    }

    @RequestMapping("/*")
    public String window_table1(){
        return "404";
    }


    @PostMapping("/backend-api-endpoint/addWindow")
    @ResponseBody
    public ResponseEntity<String> addWindow(@RequestBody Window window) {
        try {
            // 在这里处理新增窗口的逻辑，将 window 对象保存到数据库
            windowService.addWindow(window);
            return ResponseEntity.ok("窗口添加成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("添加窗口时出错");
        }
    }



    @PostMapping("/backend-api-endpoint/editWindow")
    @ResponseBody
    public ResponseEntity<String> editWindow(@RequestBody Window window) {
        try {
            windowService.editWindow(window);
            return ResponseEntity.ok("窗口编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("编辑窗口时出错");
        }
    }



    @PostMapping("/backend-api-endpoint/deleteWindow")
    @ResponseBody
    public ResponseEntity<String> deleteWindow(@RequestBody Map<String, String> requestData) {
        String windowId = requestData.get("window_id");

        try {
            windowService.deleteWindow(windowId);
            return ResponseEntity.ok("窗口删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("删除窗口时出错");
        }
    }



    @GetMapping("/backend-api-endpoint/getWindowById")
    @ResponseBody
    public ResponseEntity<Window> getWindowById(@RequestParam String windowId) {
        try {
            Window window = windowService.getWindowById(windowId);
            System.out.println("Retrieved window: " + window);
            return ResponseEntity.ok(window);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    @GetMapping("/edit_window_with_params")
    public String editWindowPage(Model model, @RequestParam String windowId) {
        // 假设你在 WindowService 中有一个通过 ID 获取消费者的方法
        Window window = windowService.getWindowById(windowId);
        // 将 window 对象添加到模型中
        model.addAttribute("window", window);
        model.addAttribute("windowId", windowId);
        // 返回 Thymeleaf 模板名称
        return "edit_window";
    }






}
